import Settings
import Log
import time,os,json
import requests

class Notify():

	def run(self):
		# 判断是否有应用中心的通知信息
		Notify = self.loadJson('Notify')
		for item in Notify:
			self.sendNotify('DiscuzAddon应用中心 通知', item)
			time.sleep(1)
		self.saveJsons('Notify', [])

	def loadJson(self, t):
		filename = self.getJsonUrl(t)
		jsonfile = open(filename, 'r+')
		jsonstr = jsonfile.read()
		JsonDta = json.loads(jsonstr)
		return JsonDta

	def saveJsons(self, t, data):
		JsonData = json.dumps(data)
		filename = self.getJsonUrl(t)
		with open(filename, 'w', encoding='utf8') as file:
			file.write(JsonData)
		return filename

	def getJsonUrl(self, t):
		filename = Settings.JsonDir + "%s.json" % t
		if os.path.exists(Settings.JsonDir) is False:
			os.mkdir(Settings.JsonDir)
		if os.path.isfile(filename) is False:
			with open(filename, 'w', encoding='utf8') as file:
				file.write('[]')
			Log.Log().rwitelog('写入默认初始文件 %s' % (filename))
		return filename

	def sendNotify(self, title, content):
		data = {
		   "text":title,
		   "desp":content
		}
		requests.post(Settings.FtqqApi,data = data)
		Log.Log().rwitelog('发送消息到微信（方糖）：%s' % content)

Notify().run()